package com.ysd.salepromotion.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import com.ysd.common.entity.Product;
import com.ysd.salepromotion.common.JSONResult;
import com.ysd.salepromotion.common.ResultList;
import com.ysd.salepromotion.param.in.AddActiveRuleRequestType;
import com.ysd.salepromotion.param.in.QueryAllActiveRequestType;
import com.ysd.salepromotion.param.out.QueryAllActiveResponseType;
import com.ysd.salepromotion.service.SalePromotionService;
import com.ysd.salepromotion.util.StringUtil;

@RestController
@RequestMapping("/salePromotion")
public class SalePromotionController {
	@Value("${server.port}")
	private String port;
	
	@Value("${spring.cloud.client.ip-address}")
	private String ip;
	
	@Autowired
	private SalePromotionService salePromotionService;
	
	/**
	 * 添加活动规则
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/addActive")
	public @ResponseBody JSONResult addActiveRule(@RequestBody AddActiveRuleRequestType request) {
		return salePromotionService.addActiveRule(request);
	}
	/**
	 * 删除活动
	 * @param request
	 * @return
	 */
	@GetMapping("/delActive/{aId}")
	public ResultList delActive(@PathVariable long aId) {
        return salePromotionService.delActive(aId);
	}
	
	/**
	 * 删除活动规则（）
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/updateActive")
	public JSONResult updateActive(@RequestBody AddActiveRuleRequestType request) {
		return salePromotionService.updateActive(request);
	}
	
	@RequestMapping(value = "/getAllActive")
	public ResultList queryAllActives(@RequestBody QueryAllActiveRequestType request) {
		return salePromotionService.queryAllActives(request);
	}
	
	
	@RequestMapping(value = "/queryAllActivesGroup", method = RequestMethod.POST)
	public ResultList queryAllActivesGroup(HttpServletRequest request) {
        return salePromotionService.queryAllActivesGroup(request);
	}
	
	
	@RequestMapping(value = "/queryAllActives01", method = RequestMethod.POST)
	public ResultList queryAllActives01(HttpServletRequest request) {
		/**
		 *  private String name;//活动名称
			private String start;//开始时间
			private String end;//结束时间
			private int activeType;//活动类型 1:满减 2:满折
			private int shelf;//是否上架 0:未上架  1:已上架
		 */
		QueryAllActiveRequestType queryAllActiveRequestType = convertBean(request);
        return salePromotionService.queryAllActives(queryAllActiveRequestType);
	}
	
	private QueryAllActiveRequestType convertBean(HttpServletRequest request) {
		QueryAllActiveRequestType queryAllActiveRequestType = new QueryAllActiveRequestType();
		queryAllActiveRequestType.setName(request.getParameter("name"));
		queryAllActiveRequestType.setBeginStartDate(request.getParameter("beginStartDate"));
		queryAllActiveRequestType.setEndStartDate(request.getParameter("endStartDate"));
		queryAllActiveRequestType.setBeginFinishDate(request.getParameter("beginFinishDate"));
		queryAllActiveRequestType.setEndFinishDate(request.getParameter("endFinishDate"));
		if(StringUtil.isNotNullOrEmpty(request.getParameter("activeType"))) {
			queryAllActiveRequestType.setActiveType(Integer.parseInt(request.getParameter("activeType")));
		}
		if(StringUtil.isNotNullOrEmpty(request.getParameter("shelf"))) {
			queryAllActiveRequestType.setShelf(Integer.parseInt(request.getParameter("shelf")));
		}
		
		queryAllActiveRequestType.setLimit(Integer.parseInt(request.getParameter("limit")));
		queryAllActiveRequestType.setPage(Integer.parseInt(request.getParameter("page")));
		return queryAllActiveRequestType;
	}
	
	//添加秒杀商品接口Product
	@RequestMapping(value = "/addMsProductList", method = RequestMethod.POST)
	public ResultList addMsProductList(@RequestBody List<Product> request) {
        return salePromotionService.addMsProductList(request);
	}
	
	//根据活动ID查询该秒杀活动的所有商品
	//添加秒杀商品接口Product
	@RequestMapping(value = "/queryProListByAId/{activeId}", method = RequestMethod.GET)
	public ResultList addMsProductList(@PathVariable String activeId) {
	    return salePromotionService.queryProListByAId(activeId);
	}
	
	//查询所有今天秒杀的商品
	@RequestMapping(value = "/queryMSProList", method = RequestMethod.POST)
	public ResultList queryMSProList() {
	    return salePromotionService.queryMSProList();
	}
	
	//删除秒杀商品
	//查询所有今天秒杀的商品
	@RequestMapping(value = "/delMSProList/{msPid}", method = RequestMethod.POST)
	public ResultList delMSProList(@PathVariable long msPid) {
	    return salePromotionService.delMSProList(msPid);
	}
	

}
